from rest_framework import serializers
from .sign_models import Sign, SignCompose
from rest_framework.fields import SerializerMethodField


class SignSeriallzers(serializers.ModelSerializer):
    class Meta:
        model = Sign
        fields = ['id', 'name']


class SignComposeSeriallzers(serializers.ModelSerializer):
    sign = SerializerMethodField()

    class Meta:
        model = SignCompose
        fields = ['id', 'name', 'sign']

    def get_sign(self, obj):
        signs = []
        value = obj.sign
        if isinstance(value, int):
            signs.append(self.queryset.get(id=value).name)
            return signs
        else:
            id_list = value.split(",")
            for id in id_list:
                try:
                    signs.append(Sign.objects.get(id=id).name)
                except:
                    pass
            return signs
